/**   
 * @Title: SportteryMainThread.java 
 * @Package: com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: 竞彩篮球，竞彩足球主界面刷新线程
 * @Author: Kuroky chengxt@500wan.com
 * @Date: 2011-7-5 下午02:26:19  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import java.util.ArrayList;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.model.sportterybasketball.SportteryBasketballAgainstInfo;
import com.esun.model.sportteryfootball.SportteryFootballAgainstInfo;
import com.esun.rabbit2.communicaters.sportterybasketball.SportteryBasketballNoFilterAgainstInfoCommunicater;
import com.esun.rabbit2.communicaters.sportteryfootball.SportteryFootballNoFilterAgainstInfoCommunicater;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

/**
 * @ClassName: SportteryMainThread
 * @Description: 竞彩篮球，竞彩足球主界面刷新线程
 * @Author: Kuroky chengxt@500wan.com
 * @Date: 2012-02-27 下午02:26:19
 * 
 */
public class SportteryMainThread {

	private LinearLayout itemView;
	private String lotid;
	private ProgressBar pb;

	private Handler handler;

	// 消息获取成功
	private static final int GET_TERM_SUCCESS = 10001;
	// 消息获取失败
	private static final int GET_TERM_FAILED = 10002;

	private ArrayList<SportteryFootballAgainstInfo> footballAgainstInfo = new ArrayList<SportteryFootballAgainstInfo>();
	private ArrayList<SportteryBasketballAgainstInfo> basketballAgainstInfo = new ArrayList<SportteryBasketballAgainstInfo>();

	// 竞彩足球获取对阵信息类
	private SportteryFootballNoFilterAgainstInfoCommunicater communicater = new SportteryFootballNoFilterAgainstInfoCommunicater();
	// 竞彩篮球获取对阵信息类
	private SportteryBasketballNoFilterAgainstInfoCommunicater basketballCommunicater = new SportteryBasketballNoFilterAgainstInfoCommunicater();

	private boolean isCanbuy = false;
	private boolean isRunning = false;
	// 是否是第一次加载
	private boolean isFirstLoading = true;

	public SportteryMainThread(LinearLayout itemView, ProgressBar progressBar, String lotid) {
		this.itemView = itemView;
		this.lotid = lotid;
		this.pb = progressBar;

		initHandler();
	}

	/*
	 * 
	 */
	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				switch (msg.what) {
				case GET_TERM_SUCCESS:
					isCanbuy = true;
					isRunning = false;

					if (lotid.equals(Config.JINGCAIZUQIU) && footballAgainstInfo.size() != 0) {
						itemViewVisiable();
						// 获取成功操作
						showInfoTermSuccess();
					} else if (lotid.equals(Config.JINGCAILANQIU) && basketballAgainstInfo.size() != 0) {
						itemViewVisiable();
						// 获取成功操作
						showInfoTermSuccess();
					} else {
						// 获取服务器信息失败
						showInfoTermFail();
					}

					break;

				case GET_TERM_FAILED:
					isCanbuy = false;
					isRunning = false;

					// 获取服务器信息失败
					showInfoTermFail();

					break;

				default:
					break;
				}
			}
		};
	}

	/*
	 * 加载成功设置
	 */
	private void showInfoTermSuccess() {

		// 进度条不可见
		progressBarGone();
		// 设置显示球队
		setTeam();
		// 球队信息可见
		itemViewVisiable();
	}

	/*
	 * 加载失败设置
	 */
	public void showInfoTermFail() {

		// 进度条不可见
		progressBarGone();
		itemViewGone();
	}

	/*
	 * 刷新线程
	 */
	public void refresh() {

		// stopFlag = flag ;
		if (!isRunning) {

			isCanbuy = false;

			// 第一次加载load页面
			if (isFirstLoading) {
				itemViewGone();
				isFirstLoading = false;
			}
			progressBarVisiable();

			// 获取期号及截止时间线程
			new Thread() {

				@Override
				public void run() {
					super.run();
					isRunning = true;

					if (lotid.equals(Config.JINGCAIZUQIU)) {
						// 获取竞彩足球信息
						if (!doGetFootballInfo()) {
							try {
								Thread.sleep(3000);
							} catch (InterruptedException e) {
							}
						}
					} else {

						// 获取竞彩篮球信息
						if (!doGetBasketballInfo()) {
							try {
								Thread.sleep(3000);
							} catch (InterruptedException e) {
							}
						}
					}
				}
			}.start();
		}
	}

	/*
	 * 获取竞彩足球队伍信息
	 */
	private boolean doGetFootballInfo() {

		try {
			// 获取成功
			if (communicater.isGetInfoSucceed(2,1)) {

				footballAgainstInfo = communicater.getAgainstInfoList();

				Message msg = handler.obtainMessage(GET_TERM_SUCCESS);
				handler.sendMessage(msg);
				return true;
			} else {
				// 获取失败
				Message msg = handler.obtainMessage(GET_TERM_FAILED);
				handler.sendMessage(msg);
				return false;
			}
		} catch (Exception e) {
			// 获取失败
			Message msg = handler.obtainMessage(GET_TERM_FAILED);
			handler.sendMessage(msg);
			return false;

		}
	}

	/*
	 * 获取竞彩篮球队伍信息
	 */
	private boolean doGetBasketballInfo() {

		try {
			// 获取成功
			if (basketballCommunicater.isGetInfoSucceed(2)) {

				basketballAgainstInfo = basketballCommunicater.getAgainstInfoList();

				Message msg = handler.obtainMessage(GET_TERM_SUCCESS);
				handler.sendMessage(msg);

				return true;
			} else {
				// 获取失败
				Message msg = handler.obtainMessage(GET_TERM_FAILED);
				handler.sendMessage(msg);
				return false;
			}
		} catch (Exception e) {
			// 获取失败
			Message msg = handler.obtainMessage(GET_TERM_FAILED);
			handler.sendMessage(msg);
			return false;

		}
	}

	/*
	 * 设置信息可见
	 */
	private void itemViewVisiable() {
		itemView.setVisibility(View.VISIBLE);
	}

	/*
	 * 设置信息不可见
	 */
	private void itemViewGone() {
		itemView.setVisibility(View.GONE);
	}

	/*
	 * 进度条可见
	 */
	public void progressBarVisiable() {
		pb.setVisibility(View.VISIBLE);
	}

	/*
	 * 进度条不可见
	 */
	public void progressBarGone() {
		pb.setVisibility(View.GONE);
	}

	public boolean isCanbuy() {
		return this.isCanbuy;
	}

	/*
	 * 设置球队
	 */
	private void setTeam() {

		TextView tvHomeTeam;
		TextView tvAwayTeam;
		String homeTeam;
		String awayTeam;

		// 竞彩足球
		if (lotid.equals(Config.JINGCAIZUQIU)) {
			EDebug.o(this, "print by billy =>> !!!!!!!!!!eeeeeeeeeeee!!!!!!!" + lotid);
			tvHomeTeam = (TextView) itemView.findViewById(R.id.lotteryhallact_tv_toSportteryFootball_home_team);
			tvAwayTeam = (TextView) itemView.findViewById(R.id.lotteryhallact_tv_toSportteryFootball_guest_team);
			homeTeam = footballAgainstInfo.get(0).getHomeSxName();
			awayTeam = footballAgainstInfo.get(0).getAwaySxName();
			EDebug.o(this, "print by billy =>> " + footballAgainstInfo.get(0).getAwaySxName() + "====" + footballAgainstInfo.get(0).getHomeSxName());

			// 竞彩篮球
		} else {
			tvHomeTeam = (TextView) itemView.findViewById(R.id.lotteryhallact_tv_toSportteryBasketball_home_team);
			tvAwayTeam = (TextView) itemView.findViewById(R.id.lotteryhallact_tv_toSportteryBasketball_guest_team);
			homeTeam = basketballAgainstInfo.get(0).getHomeSxName();
			awayTeam = basketballAgainstInfo.get(0).getAwaySxName();
		}

		tvHomeTeam.setText(homeTeam);
		tvAwayTeam.setText(awayTeam);
		EDebug.o(this, "print by billy =>> " + homeTeam + "====" + awayTeam);
	}

}
